﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;



public class DebugLine
{
    public class VertexList
    {
        public List<Vector3> lstVertex = new List<Vector3>();
    }

    private static List<VertexList> _lstShowLine = new List<VertexList>();
    private static Material lineMaterial;
    private static Color _mainColor = new Color(0f, 1f, 0f, 1f);

    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
                "SubShader { Pass { " +
                "    Blend SrcAlpha OneMinusSrcAlpha " +
                "    ZWrite Off Cull Off Fog { Mode Off } " +
                "    BindChannels {" +
                "      Bind \"vertex\", vertex Bind \"color\", color }" +
                "} } }");
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
        }
    }

    public static void UpdateShow()
    {
        if (_lstShowLine.Count > 0)
        {            
            CreateLineMaterial();
            lineMaterial.SetPass(0);
            

            GL.Begin(GL.LINES);
            GL.Color(_mainColor);

            foreach (VertexList vl in _lstShowLine)
            {
                foreach (Vector3 v in vl.lstVertex)
                {
                    GL.Vertex3(v.x, v.y, v.z);
                }
            }

            GL.End();

            _lstShowLine.Clear();
        }
    }

    public static void DrawCircleXY( Vector3 center, float radius )
    {
        VertexList lstTempVertex = new VertexList();        

        for (int i = 0; i < 100; ++i)
        {
            double degree = 360 * ( 0.01f * (float)i );
            double radian = degree * System.Math.PI / 180;
            
            float x = center.x + (float)System.Math.Cos(radian);
            float y = center.y + (float)System.Math.Sin(radian);

            lstTempVertex.lstVertex.Add(new Vector3(x, y, 0));
        }


        _lstShowLine.Add(lstTempVertex);
    }
}
